package com.cn.wanxi.rabbitmq.config;

import com.rabbitmq.client.AMQP;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * @ClassName RabbitConfig
 * @Description rabbitMQ的配置文件类
 * @Author JiJiang
 * @Date 2022/9/27 23:22
 * @Version 1.0
 */

@Configuration
@Slf4j
public class RabbitConfig {
    private static String TOPIC_EXCHANGE = "wx.in_home.topic";

    @Bean
    public Queue hrQueue() {

        log.info("hrQueue 实例化成功...");
        // 如果rabbit 里没有这个队列，会创建该队列
        return new Queue("wx_hr", true);
    }

    @Bean
    public Queue teachersQueue() {
        log.info("teachersQueue 实例化成功...");
        // 如果rabbit 里没有这个队列，会创建该队列
        return new Queue("wx_teachers", true);
    }

    @Bean
    public Queue studentsQueue() {
        log.info("studentsQueue 实例化成功...");
        // 如果rabbit 里没有这个队列，会创建该队列
        return new Queue("wx_students", true);
    }


    @Bean
    TopicExchange wxInHomeTopicExchange() {
        // 如果rabbit 里没有这个exchange，则会创建一个
        log.info("Topic 实例化成功...,name={}", TOPIC_EXCHANGE);
        return new TopicExchange(TOPIC_EXCHANGE, true, false);
    }

    /*
        绑定队列到 wx.in_home.topic exchange.这里的名字取得不太好。
     */
    @Bean
    Binding hrQueue2WxInHomeTopic() {
        // 绑定队列到topic. 这里的routingKey是binding key.
        log.info(" hrQueue 绑定 Topic 成功");
        return BindingBuilder.bind(hrQueue()).to(wxInHomeTopicExchange()).with("wx.#");
    }

    @Bean
    Binding studentsQueue2WxInHomeTopic() {
        // 绑定队列到topic,这里的routingKey是binding key.
        log.info(" studentsQueue 绑定 Topic 成功");
        return BindingBuilder.bind(studentsQueue()).to(wxInHomeTopicExchange()).with("wx.students.*");
    }

    @Bean
    Binding teachersQueue2WxInHomeTopic() {
        // 绑定队列到topic,这里的routingKey是binding key.
        log.info(" teachersQueue 绑定 Topic 成功");
        return BindingBuilder.bind(teachersQueue()).to(wxInHomeTopicExchange()).with("wx.teachers.*");
    }

}
